/**
 * Created by Allen on 2018/2/1.
 */
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import {NzMessageService, UploadFile} from 'ng-zorro-antd';
import { _HttpClient } from '@delon/theme';
import {AppUrl} from "../../app.url";

@Component({
    selector: 'merber-center',
    templateUrl: './member-center.component.html',
    styleUrls: [ './member-center.component.less'],
    encapsulation: ViewEncapsulation.Emulated
})
export class MemberCenterComponent implements OnInit {

    list: any[] = [ null ];
    username: string;

    loading = false;
    avatarUrl: string= AppUrl.MemberHeadImg.downUrl+ '?type=0';
    uploadUrl: string = AppUrl.MemberHeadImg.upoadUrl;
//    dwnloadUrl: string = AppUrl.MemberHeadImg.downUrl+ '?type=0';


    beforeUpload = (file: File) => {
        const isJPG = file.type === 'image/jpeg';
        if (!isJPG) {
            this.msg.error('You can only upload JPG file!');
        }
        const isLt2M = file.size / 1024 / 1024 < 2;
        if (!isLt2M) {
            this.msg.error('Image must smaller than 2MB!');
        }
        return isJPG && isLt2M;
    }

    private getBase64(img: File, callback: (img: any) => void) {
        const reader = new FileReader();
        reader.addEventListener('load', () => callback(reader.result));
        reader.readAsDataURL(img);
    }

    handleChange(info: { file: UploadFile }) {
        if (info.file.status === 'uploading') {
            this.loading = true;
            return;
        }
        if (info.file.status === 'done') {
            // Get this url from response in real world.
            this.getBase64(info.file.originFileObj, (img: any) => {
                this.loading = false;
                this.avatarUrl = img;
            });
        }
    }


    constructor(private http: _HttpClient, public msg: NzMessageService) {
        this.username = localStorage.getItem('username');
    }
    todoData: any[] = [
        {startDate: '2018-01-25',
            endDate: '2018-03-25',
            title: '2018年浙商大食品&共管学院十佳歌手比赛',
            copName: '苏立生',
            company: '昆明南天电脑系统有限公司',
            department: '创业研发部',
            phone: '0871-8767666',
            amount: 20000},
        {startDate: '2018-02-15',
            endDate: '2018-03-21',
            title: '云南大学 校友助力计划',
            copName: '王珂',
            company: '云南南天信息产业股份有限公司',
            department: '业务发展部',
            phone: '0871-8767666',
            amount: 50000},
        {startDate: '2018-02-03',
            endDate: '2018-02-04',
            title: '云南财经大学足球院际杯',
            copName: 'Allen',
            company: '云南白药股份有限公司',
            department: '公关部',
            phone: '0871-8767666',
            amount: 3000}
    ];
    ngOnInit() {
        this.loading = true;
        this.http.get('/api/list', { count: 8 }).subscribe((res: any) => {
            this.list = this.list.concat(res);
            this.loading = false;
            console.log(this.list);
        });


    }
}
